class DB {
  save<T>(key: string, value: T) {
    localStorage.setItem(key, JSON.stringify(value))
  }

  /**
   *
   * @param key - key
   * @param defaultValueFn - factory fn to produce default value
   */
  get<T>(key: string, defaultValueFn: () => T): T {
    let data = localStorage.getItem(key)
    if (Object.is(data, null)) {
      return defaultValueFn()
    }
    return JSON.parse(data as string)
  }

  remove(key: string) {
    localStorage.removeItem(key)
  }

  clear() {
    localStorage.clear()
  }
}

const db = new DB()

export default db
